package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.common.exception.CustormException;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.mapper.CategoryMapper;
import com.example.reggie.service.CategoryServcie;
import com.example.reggie.service.DishService;
import com.example.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryServcie {
  @Autowired
   private DishService dishService;
  @Autowired
  private SetmealService setmealService;
    @Override
    public boolean remove(Long id) {
        LambdaQueryWrapper<Dish> dishQuery = new LambdaQueryWrapper<>();
          dishQuery.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishQuery);
        if(dishCount>0)
        {
            throw  new CustormException("关联菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<>();
           setmealQuery.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmealQuery);
        if(setmealCount>0)
        {
            throw  new CustormException("关联套餐，不能删除");
        }
        return this.removeById(id);

    }
}
